"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dolphin.sql_mode参数值
Case Name   : dolphin下sql_mode不包含no_zero_date时，插入0值
Create At   : 2023.04.10
Owner       : z@wan005
Description :
    1.创建兼容B库
    2.B库下设置参数不含no_zero_date，建表并插入0值
    3.查询表数据
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Compatibility(unittest.TestCase):
    def setUp(self):
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.database = 'd_compatibility0001'
        self.table = 't_compatibility0001'

    def test_date(self):
        text1 = 'step1:创建兼容B库;expect:成功'
        self.log.info(text1)
        create_database = self.pri_sh.execut_db_sql(
            f'''drop database if exists {self.database};
            create database {self.database} dbcompatibility='B';''')
        self.log.info(create_database)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_database,
                      '执行失败' + text1)

        text2 = 'step2:B库下设置参数不含no_zero_date，建表并插入0值;expect:成功'
        self.log.info(text2)
        sql_cmd = f"set dolphin.sql_mode='sql_mode_strict," \
                  f"sql_mode_full_group';" \
                  f"drop table if exists {self.table};" \
                  f"create table {self.table}(v date);" \
                  f"insert {self.table} values(0);" \
                  f"insert {self.table} values('0000-00-00');"
        create_table = self.pri_sh.execut_db_sql(sql_cmd,
                                                 dbname=self.database)
        self.log.info(create_table)
        self.assertIn('SET', create_table, '执行失败' + text2)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, create_table,
                      '执行失败' + text2)
        self.assertEqual(2,
                         create_table.count(self.constant.INSERT_SUCCESS_MSG),
                         '执行失败' + text2)

        text3 = 'step3:查询表数据;expect:成功'
        self.log.info(text3)
        sql_cmd = self.pri_sh.execut_db_sql(
            f"select * from {self.table};", dbname=self.database)
        self.log.info(sql_cmd)
        self.assertTrue(len(sql_cmd.strip().splitlines()) >= 5, '行数不到5行')
        self.assertEqual(2, sql_cmd.count('0000-00-00'), '执行失败' + text3)

    def tearDown(self):
        text4 = 'step4:清理环境 expect:成功'
        self.log.info(text4)
        clean_environment1 = self.pri_sh.execut_db_sql(
            f"reset dolphin.sql_mode;drop database {self.database};")
        self.log.info(clean_environment1)
        self.assertIn('RESET', clean_environment1, '执行失败' + text4)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, clean_environment1,
                      '执行失败' + text4)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
